package com.capgemini.cn.authority.rest.resource;

import com.capgemini.cn.authority.rest.exception.AuthRestBusinessException;
import com.capgemini.cn.authority.rest.valueobject.DataResponse;
import com.capgemini.cn.authority.rest.valueobject.DataStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.List;

/**
 * Created at 2017/9/18
 *
 * @param <D> 定义返回的值对象数据类型
 * @author Andriy
 */
public abstract class BaseResource<D> implements CommonResource<D> {

    /**
     * 权限管理相关接口资源的前缀
     */
    public static final String RESOURCE_PREFIX = "auth-rest/";

    private final Logger logger = LoggerFactory.getLogger(BaseResource.class);

    /**
     * 系统业务异常统一处理函数
     *
     * @param e
     */
    @ExceptionHandler(value = AuthRestBusinessException.class)
    public final DataResponse<String> exceptionHandler(AuthRestBusinessException e) {
        String exceptionMsg = "发生系统业务异常!";
        if (!StringUtils.isEmpty(e.getMessage())) {
            exceptionMsg = e.getMessage();
        }
        logger.warn(exceptionMsg, e);
        return new DataResponse(DataStatus.WARN, e.getMessage(), "");
    }

    /**
     * 系统未知异常统一处理函数
     *
     * @param e
     */
    @ExceptionHandler(value = RuntimeException.class)
    public final DataResponse<String> exceptionHandler(RuntimeException e) {
        logger.error("系统出现运行时异常！", e);
        return new DataResponse(DataStatus.FAILED, e.getMessage(), "");
    }

    /**
     * 请求参数缺失异常统一处理函数
     *
     * @param e
     */
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    public final DataResponse<String> exceptionHandler(MissingServletRequestParameterException e) {
        logger.warn("缺少请求参数：{}", e.getParameterName());
        return new DataResponse(DataStatus.WARN, e.getParameterName(), "");
    }

    /**
     * 方法参数验证不合法异常处理
     *
     * @param e
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public final DataResponse<String> exceptionHandler(MethodArgumentNotValidException e) {
        String field = e.getBindingResult().getFieldError().getField();
        String fieldErrorMsg = e.getBindingResult().getFieldError().getDefaultMessage();
        String objectName = e.getBindingResult().getObjectName();
        String errorMsg = "参数" + objectName + "的属性" + field + "的值"
                + (ObjectUtils.isEmpty(fieldErrorMsg) ? "不合法!" : fieldErrorMsg);
        logger.warn(errorMsg);
        return new DataResponse(DataStatus.WARN, errorMsg, "");
    }

    @Override
    public abstract DataResponse<D> findByOne(long id);

    @Override
    public abstract DataResponse<List<D>> findAll(int page, int size, String order, Sort.Direction direction);

    @Override
    public abstract DataResponse remove(long id) throws AuthRestBusinessException;
}
